package com.cabrida.livestar.livestar;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.model.LiveAdvData;
import com.cabrida.livestar.model.MsgAdvanceData;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.tencent.rtmp.ITXLivePushListener;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXLivePushConfig;
import com.tencent.rtmp.TXLivePusher;
import com.tencent.rtmp.ui.TXCloudVideoView;

/**
 * 明星端-直播间-预播
 */
public class StarLiveRoomAdvanceActivity extends BaseActivity {
	public static final String TAG = StarLiveRoomAdvanceActivity.class
			.getSimpleName();
	private TXLivePusher mLivePusher;
	private ImageButton ib_back;
	private Button btn_goto_live;
	private Button btn_overturn;
	private Button btn_beauty;

	private boolean isBeautify = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_star_live_room_advance);
		ib_back = (ImageButton) findViewById(R.id.ib_back);
		btn_goto_live = (Button) findViewById(R.id.btn_goto_live);
		btn_overturn = (Button) findViewById(R.id.btn_overturn);
		btn_beauty = (Button) findViewById(R.id.btn_beauty);

	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		ib_back.setOnClickListener(this);
		btn_goto_live.setOnClickListener(this);
		btn_overturn.setOnClickListener(this);
		btn_beauty.setOnClickListener(this);

		int[] sdkver = TXLivePusher.getSDKVersion();
		if (sdkver != null && sdkver.length >= 3) {
			Log.d("rtmpsdk", "rtmp sdk version is:" + sdkver[0] + "."
					+ sdkver[1] + "." + sdkver[2]);
		}

		mLivePusher = new TXLivePusher(context);
		TXLivePushConfig mLivePushConfig = new TXLivePushConfig();
		mLivePusher.setConfig(mLivePushConfig);

		TXCloudVideoView mCaptureView = (TXCloudVideoView) findViewById(R.id.video_view);
		mLivePusher.startCameraPreview(mCaptureView);
		mLivePusher.setBeautyFilter(5, 3);
	}

	/** 开播房间 */
	private void OpenRoom() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_STAR_OPEN_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "openroom_response", response.toString());

						int code = JsonUtils.getInt(response, "code", -1);
						//
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,"data", new JSONObject());
							String rtmp = JsonUtils.getString(data, "rtmp", "");
							String channel = JsonUtils.getString(data,"channel", "");
							JSONObject share = JsonUtils.getJSONObject(data, "share", new JSONObject());
							String shareTitle = JsonUtils.getString(share, "title", "");
							String shareContent = JsonUtils.getString(share, "content", "");
							JSONArray mall = JsonUtils.getJSONArray(data, "mall", new JSONArray());
							ArrayList<LiveAdvData> list = JSONParser.getLiveAdvList(mall);
							Bundle  b = new Bundle();
							b.putString("rtmp", rtmp);
							b.putString("channel", channel);
							b.putString("shareTitle", shareTitle);
							b.putString("shareContent", shareContent);
							b.putSerializable("list", list);
							openActivity(StarLiveRoomActivity.class,b);
							stopRtmpPublish();
							finish();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	// 结束推流，注意做好清理工作
	public void stopRtmpPublish() {
		mLivePusher.stopCameraPreview(true); // 停止摄像头预览
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		stopRtmpPublish();
		finish();
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.ib_back:
			stopRtmpPublish();
			finish();
			break;
		case R.id.btn_goto_live:
			OpenRoom();
			break;
		case R.id.btn_overturn:
			mLivePusher.switchCamera();
			break;
		case R.id.btn_beauty:
			if (isBeautify) {
				mLivePusher.setBeautyFilter(0, 0);
			} else {
				mLivePusher.setBeautyFilter(5, 3);
			}
			isBeautify = !isBeautify;
			break;

		default:
			break;
		}
	}

}
